package main

import (
	"beego_blog/models"
	_ "beego_blog/routers"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	// "time"
)

func init() {
	dbuser := beego.AppConfig.String("dbuser")
	dbpassword := beego.AppConfig.String("dbpassword")
	dbname := beego.AppConfig.String("dbname")
	dbcharset := beego.AppConfig.String("dbcharset")
	dbmaxidel, _ := beego.AppConfig.Int("dbmaxidel") // Int会返回两个值int,err,不使用err就用_代替
	dbmaxcon, _ := beego.AppConfig.Int("dbmaxcon")

	orm.Debug = true

	models.Init()

	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("default", "mysql", dbuser+":"+dbpassword+"@/"+dbname+"?charset="+dbcharset, dbmaxidel, dbmaxcon)
}

func main() {
	o := orm.NewOrm()
	o.Using("default")

	// 查询所有
	articles := models.QueryArticleAll()
	fmt.Print(articles)

	// 查询(id)
	/*article, _ := models.QueryArticleById(8)
	fmt.Println(article.Id, article.Author)*/

	// 创建
	/*id, status := models.CreateArticle("wanger", "sfdasdf", "ccc")
	fmt.Println(id, status)*/

	// 更新
	/*var articleFields map[string]interface{}
	articleFields = make(map[string]interface{})
	articleFields["author"] = "tom"
	articleFields["title"] = "beego"
	articleFields["content"] = "hello beego"

	status := models.UpdateArticleById(11, "article", articleFields)
	fmt.Println(status)*/

	// 删除(id)
	/*status := models.DeleteArticle(10)
	fmt.Println(status)*/

	// 启用session
	beego.BConfig.WebConfig.Session.SessionOn = true

	beego.Run()
}
